/blog/Connecting to supabase from Rust in Reqwest async
Connecting to SupaBase from Rust in Reqwest async
So let’s just say you, my fair reader, are asking yourself, "How can I connect to a turnkey data and api solution from Rust?" Well it just so happens I had the same question last night and decided to give it a shot. Here it is using the Rust Reqwest crate in the async pattern:
use dotenv::dotenv; // Because we never hard code secrets, even in simple prototypes
use reqwest;
use reqwest::header::HeaderMap;
use reqwest::header::HeaderValue;
#[macro_use]
extern crate dotenv_codegen; // Excessive for such a small piece, but I like the look better
fn construct_headers() -> HeaderMap {
dotenv().ok();
let key = dotenv!("SUPABASE_KEY");
let bearer: String = format!("Bearer {}", key);
let mut headers = HeaderMap::new();
headers.insert("apikey", HeaderValue::from_str(key).unwrap());
headers.insert(
"Authorization",
HeaderValue::from_str(bearer.as_str()).unwrap(),
);
headers
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = reqwest::Client::new();
let resp = client
.get("https://use_your_own_free_tier_account_please.supabase.co/rest/v1/base_content?select=*")
.headers(construct_headers())
.send()
.await?;
println!("{:#?}", resp.text().await?);
Ok(())
}
Of course there is a Rust SDK library coming supposedly, which provides for a more GraphQL like linked query type approach. But this is easy enough for what I’m curious about. And I’d just like to add that I am really beginning to grow fond of SupaBase the more I get into it. Seems the team has made some good design decisions so far and their feature production speed is great. I hope they can layer in some good code and company maturity growth now that they have paid tiers. It’s just really nice to see a turnkey solution built on top of PostgreSQL like this, I’d like to see them succeed.